using UnityEditor;
using UnityEngine;

namespace XFFSM
{
    public class PackageInfo
    { 
        public string version;
    }

    public class FSMAboutWindow : EditorWindow
    {

        Rect textureRect = new Rect(0, 10, 291 * 0.7F, 96 * 0.7F);
        Texture logo;
        private GUIStyle style;

        private string version;

        private void Awake()
        {
            logo = AssetDatabase.LoadAssetAtPath<Texture>("Packages/com.xfkj.xffsm/Editor/Texture/logo_web.png");

            TextAsset p = AssetDatabase.LoadAssetAtPath<TextAsset>("Packages/com.xfkj.xffsm/package.json");
             if(p != null )
            {
                PackageInfo info = JsonUtility.FromJson<PackageInfo>(p.text);
                version = string.Format("Version {0}", info.version);
            }

        }

        private void ConfigStyle()
        {
            style = new GUIStyle(GUI.skin.label);
            style.richText = true;
            style.normal.textColor = new Color(0.03f, 0.4f, 0.9f, 1);
            style.onHover.textColor = Color.white;
            style.alignment = TextAnchor.MiddleLeft;
            style.fontStyle = FontStyle.Italic;
            //style.onFocused.textColor = Color.red;
        }

        // 每秒10帧更新
        void OnInspectorUpdate()
        {
            //开启窗口的重绘，不然窗口信息不会刷新
            Repaint();
        }

        private void OnGUI()
        {
            if(logo!=null)
                GUI.DrawTexture(textureRect, logo);
            GUILayout.Space(textureRect.height + 20);
            GUILayout.BeginHorizontal();
            GUILayout.Space(130);
            GUILayout.Label(version);
            GUILayout.EndHorizontal();
            GUILayout.Space(10);

            GUILayout.Label("欢迎使用XFFSM!");
            GUILayout.Label("XFFSM 是一款可视化有限状态机插件,XFFSM 能够帮助您加速开发流程,简化状态设计!");
            GUILayout.Label("如果您在使用的过程碰到任何问题 或 错误，请通过下面的QQ交流群联系到我们!");
            GUILayout.Label("感谢您的支持!");
            //GUILayout.Label("更多信息可通过点击下方教程链接获取!");
            GUILayout.Space(20);
            if (style == null)
            {
                ConfigStyle();
            }

            //DrawLink("更多教程:", "https://space.bilibili.com/258939476");
            DrawLink("插件源码:", "https://gitee.com/xianfengkeji/xffsm");
            GUILayout.Space(20);
            GUILayout.Label("XFFSM 交流群:644685781");

            //GUILayout.Space(20);
            GUILayout.Label("*弦风课堂制作");
        }

        private void DrawLink(string title, string url)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(title, GUILayout.Width(60));

            if (GUILayout.Button(url, style))
            {
                Application.OpenURL(url);
            }

            GUILayout.EndHorizontal();
        }

    }

}